import os
import random
class AdminSystem:
    def welcom(self):
        print('*' * 42)
        print('***' + ' ' * 36 + '***')
        print('***' + ' ' * 36 + '***')
        print('***' + ' ' * 8 +'欢迎登录银行管理系统' + ' ' * 8 + '***')
        print('***' + ' ' * 36 + '***')
        print('***' + ' ' * 36 + '***')
        print('*' * 42)
        print('1.管理员注册:')
        print('2.管理员登录:')
        option = input('请输入选择的功能:\n')
        if option == '1':
            self.register()
        if option == '2':
            self.login()
    def register(self):
        if os.path.exists('admin_info.txt') is not True:
            with open('admin_info.txt', mode='w+', encoding='utf-8') as f:
                f.write('')
        admin_dict = dict()
        admin_name = input('请输入注册名:')
        for i in self.data_convert():
            if i['账号'] == admin_name:
                print('该账号已存在')
                return self.register()
        admin_password = input('请输入密码')
        admin_dict['账号'] = admin_name
        admin_dict['密码'] = admin_password
        with open('admin_info.txt',mode='a+',encoding='utf-8') as f:
            f.write(str(admin_dict)+'\n')
        print('注册成功')
        return self.welcom()
    def data_convert(self):
        admin_info_li = []
        with open ('admin_info.txt',mode='r+',encoding='utf-8') as admin_info:
            for i in admin_info.readlines(): # i = {'账号':'张三','密码':'123'}
                admin_info_dict = {}
                step_1 = i.replace('{', '').replace('}', '').replace("'",'')
                step_2 = step_1.split(',')
                admin_info_dict['账号'] = step_2[0].split(':')[1].strip()
                admin_info_dict['密码'] = step_2[1].split(':')[1].strip()
                admin_info_li.append(admin_info_dict)
            print(admin_info_li)
        return admin_info_li

    def login(self):
        admin_info_li =self.data_convert()
        admin_name = input('请输入账号名:\n')
        for i in admin_info_li:
            if i['账号'] == admin_name:
                admin_password = input('请输入密码:\n')
                index_admin_name = admin_info_li.index(i)
                if admin_info_li[index_admin_name]['密码'] == admin_password:
                    print('登陆成功')
                    function = Function()
                    function.welcome()
                else:
                    print('密码不正确')
                    return self.login()
            else:
                print('账号不存在')
                return self.login()

class Function:
    def __init__(self):
        print('*' * 47)
        print('***' + ' ' * 41 + '***')
        print('***' + ' ' * 7 + '1.开户' + ' ' * 15 + '2.查询' + ' ' * 7)
        print('***' + ' ' * 7 + '3.取款' + ' ' * 15 + '4.存款' + ' ' * 7)
        print('***' + ' ' * 7 + '5.转账' + ' ' * 15 + '6.锁定' + ' ' * 7)
        print('***' + ' ' * 7 + '7.解锁' + ' ' * 15 + '退出(Q)' + ' ' * 6)
    def welcome(self):
        while True:
            option = input('请输入需要的功能编号:\n')
            if option == '1':
                self.register()
            if option == '2':
                self.inquire()
            if option == '3':
                self.withdrawal()
            if option == '4':
                self.deposit()
            if option == '5':
                self.transfer()
            if option == '6':
                self.lock()
            if option == '7':
                self.unlock()
            if option == 'Q':
                break
    def register(self):
        if os.path.exists('userinfo.txt') is not True:
            with open('userinfo.txt',mode='w',encoding='utf-8') as f:
                f.write('')
        else:
            user_name = input('请输入您的姓名:\n')
            user_id = input('请输入身份证号:\n')
            user_phone = input('请输入手机号:\n')
            user_money = input('请输入预存金额:\n')
            user_password = input('请输入登录密码:\n')
            userinfo_dict = dict()
            userinfo_dict_li = []
            userinfo_dict['姓名'] = user_name
            userinfo_dict['身份证'] = user_id
            userinfo_dict['手机'] = user_phone
            userinfo_dict['余额'] = user_money
            userinfo_dict['密码'] = user_password
            userinfo_dict['卡号'] = random.randint(100000, 999999)
            userinfo_dict['锁定状态'] = '否'
            userinfo_dict_li.append(userinfo_dict)
            print('注册成功,您的卡号为' + str(userinfo_dict['卡号']))
            with open('userinfo.txt',mode='a',encoding='utf-8') as f:
                f.write(str(userinfo_dict) + '\n')
    def data_convert(self):
        userinfo_dict_li = []
        with open('userinfo.txt', mode='r', encoding='utf-8') as userinfo:
            for i in userinfo.readlines():
                userinfo_dict = dict()# i = {'姓名': '王宙', '身份证': '43078119890415003X', '手机': '18373639263', '余额': '1000', '密码': '123456'}
                step_1 = i.replace('{','').replace('}','').replace("'",'') # step_1 = 姓名: 王宙, 身份证: 43078119890415003X, 手机: 18373639263, 余额: 1000, 密码: 123456, 卡号: 111111}
                step_2 = step_1.split(',') # step_2 = ['姓名: 王宙',' 身份证: 43078119890415003X',' 手机: 18373639263',.....]
                userinfo_dict['姓名'] = step_2[0].split(':')[1].strip()
                userinfo_dict['身份证'] = step_2[1].split(':')[1].strip()
                userinfo_dict['手机'] = step_2[2].split(':')[1].strip()
                userinfo_dict['余额'] = step_2[3].split(':')[1].strip()
                userinfo_dict['密码'] = step_2[4].split(':')[1].strip()
                userinfo_dict['卡号'] = step_2[5].split(':')[1].strip()
                userinfo_dict_li.append(userinfo_dict)
        return userinfo_dict_li

    def inquire(self):
        user_id_li = []
        user_lock_li = []
        user_id = input('请输入想要查询的卡号:\n')
        user_password_li = []
        user_info_dict_li = self.data_convert()
        for i in user_info_dict_li:
            user_id_li.append(i['卡号'])
            user_password_li.append(i['密码'])
            user_lock_li.append(i['锁定状态'])
        if user_id in user_id_li:
            index_user = user_id_li.index(user_id)
            count = 0
            while True:
                if user_lock_li[index_user] == '是':
                    print('账号已锁定,请先解锁')
                    break
                else:
                    user_password = input('请输入查询密码:\n')
                    if user_password == user_password_li[index_user]:
                        print(user_info_dict_li[index_user])
                        break
                    else:
                        count += 1
                        print(f'请输入正确的密码,再输入错误{3-count}次后账号将被锁定')
                        if count == 3:
                            user_lock_li[index_user] = '是'
                            print('连续错误3次,账号已锁定')
                            break
        else:
            print('请输入正确的卡号')

    def withdrawal(self):
        user_id_li = []
        user_lock_li = []
        user_id = input('请输入卡号:\n')
        user_password_li = []
        user_info_dict_li = self.data_convert()
        for i in user_info_dict_li:
            user_id_li.append(i['卡号'])
            user_password_li.append(i['密码'])
        if user_id in user_id_li:
            index_user = user_id_li.index(user_id)
            count = 0
            while True:
                if user_lock_li[index_user] == '是':
                    print('账号已锁定,请先解锁')
                    break
                else:
                    user_password = input('请输入密码:\n')
                    if user_password == user_password_li[index_user]:
                        print(user_info_dict_li[index_user])
                        withdrawal_money = input('请输入取款金额:\n')
                        user_info_dict_li[index_user]['余额'] = str(
                            float(user_info_dict_li[index_user]['余额']) - float(withdrawal_money))
                        with open('userinfo.txt', mode='w', encoding='utf-8') as f:
                            for i in user_info_dict_li:
                                f.write(str(i)+'\n')
                        break
                    else:
                        count += 1
                        print(f'请输入正确的密码,再输入错误{3 - count}次后账号将被锁定')
                        if count == 3:
                            user_lock_li[index_user] = '是'
                            print('连续错误3次,账号已锁定')
                            break

    def deposit(self):
        user_id_li = []
        user_lock_li = []
        user_id = input('请输入卡号:\n')
        user_password_li = []
        user_info_dict_li = self.data_convert()
        for i in user_info_dict_li:
            user_id_li.append(i['卡号'])
            user_password_li.append(i['密码'])
        if user_id in user_id_li:
            index_user = user_id_li.index(user_id)
            count = 0
            while True:
                if user_lock_li[index_user] == '是':
                    print('账号已锁定,请先解锁')
                    break
                else:
                    user_password = input('请输入密码:\n')
                    if user_password == user_password_li[index_user]:
                        print(user_info_dict_li[index_user])
                        deposit_money = input('请输入存款金额:\n')
                        user_info_dict_li[index_user]['余额'] = str(
                            float(user_info_dict_li[index_user]['余额']) + float(deposit_money))
                        with open('userinfo.txt', mode='w', encoding='utf-8') as f:
                            for i in user_info_dict_li:
                                f.write(str(i)+'\n')
                        break
                    else:
                        count += 1
                        print(f'请输入正确的密码,再输入错误{3 - count}次后账号将被锁定')
                        if count == 3:
                            user_lock_li[index_user] = '是'
                            print('连续错误3次,账号已锁定')
                            break

    def transfer(self):
        user_id_li = []
        user_lock_li = []
        user_id = input('请输入卡号:\n')
        user_password_li = []
        user_info_dict_li = self.data_convert()
        for i in user_info_dict_li:
            user_id_li.append(i['卡号'])
            user_password_li.append(i['密码'])
        if user_id in user_id_li:
            index_user = user_id_li.index(user_id)
            count = 0
            while True:
                if user_lock_li[index_user] == '是':
                    print('账号已锁定,请先解锁')
                    break
                else:
                    user_password = input('请输入密码:\n')
                    if user_password == user_password_li[index_user]:
                        print(user_info_dict_li[index_user])
                        transfer_id = input('请问您要转给谁,请输入卡号:\n')
                        index_transfer = user_id_li.index(transfer_id)
                        transfer_money = input('请输入转账金额:\n')
                        user_info_dict_li[index_user]['余额'] = str(
                            float(user_info_dict_li[index_user]['余额']) - float(transfer_money))
                        user_info_dict_li[index_transfer]['余额'] = str(
                            float(user_info_dict_li[index_transfer]['余额']) + float(transfer_money))
                        with open('userinfo.txt', mode='w', encoding='utf-8') as f:
                            for i in user_info_dict_li:
                                f.write(str(i)+'\n')
                        break
                    else:
                        count += 1
                        print(f'请输入正确的密码,再输入错误{3 - count}次后账号将被锁定')
                        if count == 3:
                            user_lock_li[index_user] = '是'
                            print('连续错误3次,账号已锁定')
                            break
    def lock(self):
        user_id_li = []
        user_lock_li = []
        user_id = input('请输入卡号:\n')
        user_password_li = []
        user_info_dict_li = self.data_convert()
        for i in user_info_dict_li:
            user_id_li.append(i['卡号'])
            user_password_li.append(i['密码'])
            if user_id in user_id_li:
                user_index = user_id_li.index(user_id)
                if user_lock_li[user_index] == '是':
                    print('账号已锁定,请勿重复操作')
                else:
                    user_password = input('请输入密码:\n')
                    if user_password == user_password_li[user_index]:
                        user_lock_li[user_index] = '是'
                        print('账号已成功锁定')
                    else:
                        print('请输入正确的密码')
    def unlock(self):
        user_id_li = []
        user_lock_li = []
        user_id = input('请输入卡号:\n')
        user_password_li = []
        user_info_dict_li = self.data_convert()
        for i in user_info_dict_li:
            user_id_li.append(i['卡号'])
            user_password_li.append(i['密码'])
            if user_id in user_id_li:
                user_index = user_id_li.index(user_id)
                if user_lock_li[user_index] == '否':
                    print('账号未锁定,请勿重复操作')
                else:
                    user_password = input('请输入密码:\n')
                    if user_password == user_password_li[user_index]:
                        user_lock_li[user_index] = '否'
                        print('账号已成功解锁')
                    else:
                        print('请输入正确的密码')

if __name__ == '__main__':
    adminsystem = AdminSystem()
    adminsystem.welcom()
    # def inquire(self):


